package com.lcm.fas.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

import com.lcm.fas.controller.FileController;

public class MainFrame extends JFrame implements ActionListener{
	private static final long serialVersionUID = 1L;
	public static boolean darkMode = false;//默认不开深色模式
	public static String fontType = "华康楷体W5(P)";//默认字体
	public static Color fontColorAtDark = new Color(255,255,255);//深色模式下的字体颜色
	public static Color bgColorAtDart = new Color(64,64,64);//深色模式下的背景颜色
	public static Color defaultBgColor = new Color(240,240,240);//默认背景颜色
	public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public static String ROOT_PATH=null;//磁盘根目录
	private String password = "800820820"; //安全密码,用于系统格式化
	private static JDesktopPane desktopPane;
	private static JLabel localTime;
	/*菜单栏*/
	private static JMenuBar menuBar;
	/*文件管理*/
	private static JMenu fileAdminMenu;
	private static JMenuItem seeFileMenu;
	/*资源分析*/
	private static JMenu sourceAnalysMenu;
	private static JMenuItem sourceCountMenu;
	/*系统设置*/
	private static JMenu settingsMenu;
	private static JMenuItem closeMenu;
	private static JMenuItem delMenu;
	private static JMenuItem darkChangeMenu;
	/*关于我*/
	private static JMenu meMenu;
	private static JMenuItem developMenu;
	private static JMenuItem pointsMenu;
	private static JMenuItem banbenMenu;

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		setTitle("windows资源管理器 Beta");
		setIconImage(Toolkit.getDefaultToolkit().getImage(MainFrame.class.getResource("/image/文件管理.png")));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//获取屏幕的尺寸信息
		Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();//获取窗口尺寸
		setBounds(screenSize.width/5, screenSize.height/6, 1200, 700);
		
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		fileAdminMenu = new JMenu("文件管理");
		fileAdminMenu.setIcon(new ImageIcon(MainFrame.class.getResource("/image/文件管理 (1).png")));
		menuBar.add(fileAdminMenu);
		
		seeFileMenu = new JMenuItem("查看文件目录");
		seeFileMenu.setIcon(new ImageIcon(MainFrame.class.getResource("/image/查看目录.png")));
		fileAdminMenu.add(seeFileMenu);
		seeFileMenu.addActionListener(this);
		
		sourceAnalysMenu = new JMenu("资源分析");
		sourceAnalysMenu.setIcon(new ImageIcon(MainFrame.class.getResource("/image/统计.png")));
		menuBar.add(sourceAnalysMenu);
		
		sourceCountMenu = new JMenuItem("资源分类统计");
		sourceCountMenu.setIcon(new ImageIcon(MainFrame.class.getResource("/image/资源统计.png")));
		sourceAnalysMenu.add(sourceCountMenu);
		sourceCountMenu.addActionListener(this);
		
		settingsMenu = new JMenu("系统设置");
		settingsMenu.setIcon(new ImageIcon(MainFrame.class.getResource("/image/系统设置 .png")));
		menuBar.add(settingsMenu);
		
		closeMenu = new JMenuItem("关闭程序");
		closeMenu.setIcon(new ImageIcon(MainFrame.class.getResource("/image/退出.png")));
		settingsMenu.add(closeMenu);
		closeMenu.addActionListener(this);
		
		delMenu = new JMenuItem("磁盘格式化");
		delMenu.setIcon(new ImageIcon(MainFrame.class.getResource("/image/格式化.png")));
		settingsMenu.add(delMenu);
		delMenu.addActionListener(this);
		
		darkChangeMenu = new JMenuItem("深色模式打开/关闭");
		darkChangeMenu.setIcon(new ImageIcon(MainFrame.class.getResource("/image/夜间模式.png")));
		settingsMenu.add(darkChangeMenu);
		darkChangeMenu.addActionListener(this);
		
		meMenu = new JMenu("关于我");
		meMenu.setIcon(new ImageIcon(MainFrame.class.getResource("/image/关于我们.png")));
		menuBar.add(meMenu);

		developMenu = new JMenuItem("开发者");
		developMenu.setIcon(new ImageIcon(MainFrame.class.getResource("/image/开发者 (1).png")));
		meMenu.add(developMenu);
		developMenu.addActionListener(this);
		
		pointsMenu = new JMenuItem("意见反馈");
		pointsMenu.setIcon(new ImageIcon(MainFrame.class.getResource("/image/意见反馈.png")));	
		meMenu.add(pointsMenu);
		pointsMenu.addActionListener(this);
		
		banbenMenu = new JMenuItem("版本号：1.0.1");
		banbenMenu.setIcon(new ImageIcon(MainFrame.class.getResource("/image/系统版本.png")));
		meMenu.add(banbenMenu);
		
		JLabel space = new JLabel("                          ");
		menuBar.add(space);
		
		localTime = new JLabel("0000-00-00 00:00:00");
		menuBar.add(localTime);
		
		JLabel lblNewLabel_2 = new JLabel("      数据无价，谨慎操作");
		lblNewLabel_2.setForeground(new Color(255, 0, 51));
		lblNewLabel_2.setFont(new Font(fontType, Font.BOLD, 20));
		menuBar.add(lblNewLabel_2);
		
		desktopPane = new JDesktopPane();
		
		JLabel lblNewLabel = new JLabel("欢迎使用window资源管理器 Beta版");
		lblNewLabel.setForeground(new Color(255, 255, 255));
		lblNewLabel.setFont(new Font(fontType, Font.BOLD, 40));
		lblNewLabel.setIcon(new ImageIcon(MainFrame.class.getResource("/image/文件管理 (4).png")));
		GroupLayout gl_desktopPane = new GroupLayout(desktopPane);
		gl_desktopPane.setHorizontalGroup(
			gl_desktopPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_desktopPane.createSequentialGroup()
					.addGap(220)
					.addComponent(lblNewLabel)
					.addContainerGap(227, Short.MAX_VALUE))
		);
		gl_desktopPane.setVerticalGroup(
			gl_desktopPane.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_desktopPane.createSequentialGroup()
					.addContainerGap(283, Short.MAX_VALUE)
					.addComponent(lblNewLabel)
					.addGap(277))
		);
		desktopPane.setLayout(gl_desktopPane);
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addComponent(desktopPane, GroupLayout.DEFAULT_SIZE, 982, Short.MAX_VALUE)
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(1)
					.addComponent(desktopPane, GroupLayout.DEFAULT_SIZE, 526, Short.MAX_VALUE))
		);
		getContentPane().setLayout(groupLayout);
//#######TODO  线程： 实时更新系统时间
		new Thread(new Runnable() {
			@Override
			public void run() {
				while(true) {
					try {
						localTime.setText(sdf.format(new Date()));
						Thread.sleep(1000);//一秒更新一次
					} catch (Exception e) {
						System.out.println("系统时间更新失败！！！！");
					}
				}
			}
		}).start();
		darkMode();
		
		while(ROOT_PATH==null) {
			String path = JOptionPane.showInputDialog("请输入虚拟磁盘的根目录（如E:\\D\\）\n注： 不输入或者取消输入将退出程序！且必须以\\结尾：");
			if(path==null) {
				System.exit(0);
			}
			if(path !=null & path.trim().length()>0) {
				if(path.endsWith("\\")) {
					File file = new File(path);
					if(file.exists()) {
						ROOT_PATH = path;
						break;
					}else{
						continue;
					}
				}
			}else {
				System.exit(0);
			}
		}
	}

	/**
	 * 点击菜单事件监听
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		String target = e.getActionCommand();
		switch (target) {
			case "查看文件目录":
				FileInternalFrame fileInternalFrame = new FileInternalFrame();
				desktopPane.add(fileInternalFrame);
				fileInternalFrame.setVisible(true);
				break;
			case "资源分类统计":
				FileAnalysInternalFrame fileAnalysInternalFrame = new FileAnalysInternalFrame();
				desktopPane.add(fileAnalysInternalFrame);
				fileAnalysInternalFrame.setVisible(true);
				break;
			case "关闭程序":
				int confirmClose = JOptionPane.showConfirmDialog(null, "确认要关闭程序？","温馨提示",JOptionPane.YES_NO_OPTION);
				if(confirmClose==0) {//确认关闭
					System.exit(0);
				}
				break;
			case "深色模式打开/关闭":
				JOptionPane.showMessageDialog(null, "正在"+(!darkMode?"打开":"关闭")+"深色模式");
				if(darkMode) {
					darkMode=false;
				}else {
					darkMode = true;
				}
				//动态改变背景颜色和字体颜色
				darkMode();
//				FileInternalFrame.darkMode();
				break;
			case "磁盘格式化":
				int confirmClear = JOptionPane.showConfirmDialog(null, "此操作会删除改盘下的所有目录与文件，确认要进行格式化？","温馨提示",JOptionPane.YES_NO_OPTION);
				if(confirmClear==0) {//确认格式化
					String pwd = JOptionPane.showInputDialog("请输入安全密码以确认格式化操作：");
					if(pwd!=null && pwd.length()>0) {
						if(pwd.equals(password)) {
							JOptionPane.showMessageDialog(null, "磁盘格式化操作正在进行中。。。。","温馨提示",JOptionPane.INFORMATION_MESSAGE);
							boolean status = FileController.deleteFile(ROOT_PATH);
							if(status) {
								JOptionPane.showMessageDialog(null, "磁盘格式化成功！","温馨提示",JOptionPane.INFORMATION_MESSAGE);
								FileInternalFrame.initTable(ROOT_PATH);
							}
						}else {
							JOptionPane.showMessageDialog(null, "密码错误，格式化操作已取消","温馨提示",JOptionPane.INFORMATION_MESSAGE);
						}
					}
				}else {
					break;
				}
				break;
			case "开发者":
				JOptionPane.showMessageDialog(null,"开发团队：你猜我是哪一组\n组长：李创梅\n组员：李登印、李国航\n\n 版权所有，侵权必究！","开发者信息", JOptionPane.INFORMATION_MESSAGE);
				break;
			case "意见反馈":
				try {
					Runtime.getRuntime().exec("explorer.exe mailto:1307182670@qq.com");
				} catch (IOException e1) {
					JOptionPane.showMessageDialog(null,"请联系开发者：800820820进行意见反馈","温馨提示", JOptionPane.INFORMATION_MESSAGE);
				}
				break;
			default :
				break;
		}
	}
	
	/**
	 * 设置字体样式
	 * @param component
	 */
	public static void setFontStyle(JComponent component,int fontClass) {
		switch (fontClass) {
		case 1://一级菜单
			component.setFont(new Font(fontType, Font.BOLD, 28));
			break;
		case 2://二级菜单
			component.setFont(new Font(fontType, Font.PLAIN, 25));
			break;
		case 3://三级菜单
			component.setFont(new Font(fontType, Font.PLAIN, 20));
			break;
		default: 
			break;
		}
		component.setForeground(darkMode?fontColorAtDark:new Color(0, 0, 0));//根据深色模式来调整字体颜色
	}
	
	//深色模式
	public static void darkMode() {
		//深色模式下的字体调整
		setFontStyle(fileAdminMenu,1);
		setFontStyle(seeFileMenu,2);
		setFontStyle(sourceAnalysMenu,1);
		setFontStyle(sourceCountMenu,2);
		setFontStyle(settingsMenu,1);
		setFontStyle(closeMenu,2);
		setFontStyle(delMenu,2);
		setFontStyle(darkChangeMenu,2);
		setFontStyle(meMenu,1);
		setFontStyle(developMenu,2);
		setFontStyle(pointsMenu,2);
		setFontStyle(banbenMenu,2);
		setFontStyle(localTime, 3);
		//深色模式下的背景颜色调整
		menuBar.setBackground(darkMode?bgColorAtDart:defaultBgColor);
		seeFileMenu.setBackground(darkMode?bgColorAtDart:defaultBgColor);
		sourceCountMenu.setBackground(darkMode?bgColorAtDart:defaultBgColor);
		closeMenu.setBackground(darkMode?bgColorAtDart:defaultBgColor);
		delMenu.setBackground(darkMode?bgColorAtDart:defaultBgColor);
		darkChangeMenu.setBackground(darkMode?bgColorAtDart:defaultBgColor);
		developMenu.setBackground(darkMode?bgColorAtDart:defaultBgColor);
		pointsMenu.setBackground(darkMode?bgColorAtDart:defaultBgColor);
		banbenMenu.setBackground(darkMode?bgColorAtDart:defaultBgColor);
		desktopPane.setBackground(darkMode?bgColorAtDart:new Color(0, 153, 255));
	}
	
}
